<template>
	<view class="jnpf-wrap jnpf-wrap-workflow">
		<u-form :model="dataForm" ref="dataForm" :errorType="['toast']" label-position="left" label-width="180"
			label-align="left">
			<u-divider color="#fa3534">立项信息</u-divider>
			<u-form-item label="项目编号" v-if="judgeShow('wk012')">
				<u-input v-model="dataForm.wk012" placeholder="项目编号" disabled></u-input>
			</u-form-item>
			<u-form-item label="项目名称" type="textarea" v-if="judgeShow('wk003')">
				<u-input v-model="dataForm.wk003" placeholder="项目名称" disabled></u-input>
			</u-form-item>
			<u-form-item label="优先级" type="textarea" v-if="judgeShow('wk016')">
				<u-tag v-if="dataForm.wk016 == 1" text="常规" type="primary" />
				<u-tag v-if="dataForm.wk016 == 2" text="优先" type="warning" />
				<u-tag v-if="dataForm.wk016 == 3" text="重点" type="error" />
			</u-form-item>
			<u-form-item label="项目经理" type="textarea" v-if="judgeShow('wk017')">
				<view v-for="(item, index) in wk017Name" :key="index" style="margin-left: 5px">
					<u-tag :text="item" type="success" />
				</view>
			</u-form-item>
			<u-form-item label="客户名称" type="textarea" v-if="judgeShow('wk005')">
				<u-input v-model="dataForm.wk005" placeholder="客户名称" disabled></u-input>
			</u-form-item>
			<u-form-item label="业务负责人" type="textarea" v-if="judgeShow('wk018')">
				<view v-for="(item, index) in wk018Name" :key="index" style="margin-left: 5px">
					<u-tag :text="item" type="success" />
				</view>
			</u-form-item>
			<u-form-item label="项目地点" type="textarea" v-if="judgeShow('wk008')">
				<u-input v-model="dataForm.wk008" placeholder="项目地点" disabled></u-input>
			</u-form-item>
			<u-form-item label="项目类型" type="textarea" v-if="judgeShow('wk019')">
				{{ formattedsortCode(dataForm.wk019) }}
			</u-form-item>
			<u-form-item label="发货日期" v-if="judgeShow('wk020')">
				<span>{{ formattedData(dataForm.wk020) }}</span>
			</u-form-item>
			<u-form-item label="交付日期" v-if="judgeShow('wk021')">
				<span>{{ formattedData(dataForm.wk021) }}</span>
			</u-form-item>
			<u-form-item label="关联计划" v-if="judgeShow('wk004')">
				<span>{{ dataForm.wk004 }}</span>
			</u-form-item>
			<u-form-item label="计划开始时间">
				<span>{{ formattedTime(dataForm.planStartTime) }}</span>
			</u-form-item>
			<u-form-item label="计划结束时间">
				<span>{{ formattedTime(dataForm.planEndTime) }}</span>
			</u-form-item>
			<u-form-item label="实际开始时间">
				<span>{{ formattedTime(dataForm.wk014) }}</span>
			</u-form-item>
			<u-form-item label="实际完成时间">
				<span>{{ formattedTime(dataForm.wk022) }}</span>
			</u-form-item>
			<u-divider color="#fa3534">设备对点</u-divider>
			<u-form-item label="说明" type="textarea" v-if="judgeShow('wk010')">
				<u-input v-model="dataForm.wk010" placeholder="说明" :disabled="judgeWrite('wk010')"></u-input>
			</u-form-item>

			<u-form-item label="附件" v-if="judgeShow('annex01')">
				<jnpf-file v-model="dataForm.annex01" :disabled="judgeWrite('annex01')" />
			</u-form-item>
			<u-form-item label="备注" type="textarea" v-if="judgeShow('wk023')">
				<u-input v-model="dataForm.wk023" placeholder="备注"></u-input>
			</u-form-item>
			<u-form-item label="任务处理人" type="textarea" v-if="judgeShow('wk013')">
				<view v-for="(item, index) in wk013Name" :key="index" style="margin-left: 5px">
					<u-tag :text="item" type="success" />
				</view>
			</u-form-item>
			<u-form-item label="责任人" type="textarea" v-if="judgeShow('wk015')">
				<view v-for="(item, index) in wk015Name" :key="index" style="margin-left: 5px">
					<u-tag :text="item" type="success" />
				</view>
			</u-form-item>
		</u-form>
	</view>
</template>
<script>
	import {
		getPjswk
	} from "@/api/workFlow/flowEngine";
	import {
		getUserAll,
		Interface,
		getDictionaryDataSelector
	} from "@/api/common";
	import comMixin from "../mixin";
	import {
		getGoodsList,
		getCustomerList,
		Info,
		Create,
		Update,
	} from "@/api/apply/order";
	export default {
		name: "crmOrder",
		mixins: [comMixin],
		data() {
			return {
				childrenShow: false,
				childrenName: "",
				dataForm: {},
				tableField133: [],
				id: "",
				wk017Name: [],
				wk018Name: [],
				wk013Name: [],
				wk015Name: [],
				nameList: [],
				xx032List: [],
				tn003List: [],
				childrenList: [],
			};
		},

		methods: {
			init(data, id, flowFormContentJson) {
				this.setting = data;
				this.id = id;
				this.getInfo(data);
			},
			childNodes(item) {
				if (item.children.length == 0) return;
				this.childrenList = item.children;
				this.childrenName = item.tb004 + "-子节点";
				this.childrenShow = true;
			},
			getDynamicStyle(item) {
				return {
					"background-color": item.children && item.children.length > 0 ? "aqua" : "",
				};
			},
			// 转换时间到时
			formattedTime(time = "0000000000") {
				if (!time) return "";
				return (
					time.slice(0, 4) +
					"-" +
					time.slice(4, 6) +
					"-" +
					time.slice(6, 8) +
					" " +
					time.slice(8, 10)
				);
			},
			// 转换时间到日
			formattedData(data = "00000000") {
				if (!data) return "";
				return data.slice(0, 4) + "-" + data.slice(4, 6) + "-" + data.slice(6, 8);
			},
			formattedName(id) {
				// 找到匹配的ID并返回名字
				const person = this.nameList.find((p) => p.id === id);
				return person ? person.realName : "";
			},
			formattedtn003(tn003) {
				// 找到匹配的ID并返回名字
				const obj = this.tn003List.find((p) => p.id === tn003);
				return obj ? obj.fullName : "";
			},
			formattedsortCode(xx032) {
				const obj = this.xx032List.find((p) => p.sortCode == xx032);
				return obj ? obj.fullName : "";
			},
			getInfo(data) {
				console.log('data', data.id);
				getPjswk(data.id).then((res) => {
					console.log('res', res);
					if (res.code == 200) {
						this.dataForm = res.data;
						console.log(this.dataForm);
						this.getUser();
						this.getInterface();
						this.getxz008Options();
					}
				});
			},
			// 获取项目类型
			getxz008Options() {
				getDictionaryDataSelector("505518848196226117").then((res) => {
					this.xx032List = res.data.list;
				});
			},
			// 获取优先级
			getxz009Options() {
				getDictionaryDataSelector("505518936847035461").then((res) => {});
			},
			getUser() {
				getUserAll().then((res) => {
					this.nameList = res.data;
					this.wk017Name = res.data
						.filter((item) => this.dataForm.wk017.includes(item.id))
						.map((item) => item.realName);

					this.wk018Name = res.data
						.filter((item) => this.dataForm.wk018.includes(item.id))
						.map((item) => item.realName);

					this.wk013Name = res.data
						.filter((item) => this.dataForm.wk013.includes(item.id))
						.map((item) => item.realName);
					this.wk015Name = res.data
						.filter((item) => this.dataForm.wk015.includes(item.id))
						.map((item) => item.realName);
				});
			},
			getInterface() {
				Interface().then((res) => {
					this.tn003List = res.data;
				});
			},
			selfGetInfo() {
				Info(this.setting.id).then((res) => {
					this.dataForm = res.data;
					if (this.dataForm.fileJson) {
						this.fileList = JSON.parse(this.dataForm.fileJson);
					}
				});
			},
			selfSubmit() {
				this.dataForm.status = this.eventType === "submit" ? 0 : 1;
				if (this.eventType === "save") return this.selfHandleRequest();
				uni.showModal({
					title: "提示",
					content: "您确定要提交当前流程吗？",
					success: (res) => {
						if (res.confirm) {
							this.selfHandleRequest();
						}
					},
				});
			},
			selfHandleRequest() {
				if (!this.dataForm.id) delete this.dataForm.id;
				const formMethod = this.dataForm.id ? Update : Create;
				formMethod(this.dataForm).then((res) => {
					uni.showToast({
						title: res.msg,
						complete: () => {
							setTimeout(() => {
								uni.$emit("refresh");
								uni.navigateBack();
							}, 1500);
						},
					});
				});
			},
			initData() {
				getGoodsList().then((res) => {
					this.goodsOptions = res.data.list;
				});
				getCustomerList().then((res) => {
					this.customerOptions = res.data.list;
				});
			},
			selfInit() {
				this.addGoods();
				this.addPlan();
			},
			onChange(e) {
				const user = e[0];
				this.dataForm.salesmanName = user.fullName;
			},
			addGoods() {
				const item = {
					goodsId: "",
					goodsCode: "",
					goodsName: "",
					specifications: "",
					unit: "",
					qty: "",
					price: "",
					amount: "",
					discount: null,
					cess: null,
					actualPrice: "",
					actualAmount: "",
					description: "",
				};
				this.dataForm.goodsList.push(item);
			},
			delGoods(index) {
				this.dataForm.goodsList.splice(index, 1);
			},
			addPlan() {
				const item = {
					receivableDate: "",
					receivableRate: "",
					receivableMoney: "",
					receivableMode: "",
					abstract: "",
				};
				this.dataForm.collectionPlanList.push(item);
			},
			delPlan(index) {
				this.dataForm.collectionPlanList.splice(index, 1);
			},
			onGoodsChange(e, i) {
				const goods = e[0];
				this.dataForm.goodsList[i] = {
					goodsId: goods.id,
					goodsCode: goods.code,
					goodsName: goods.text,
					specifications: goods.specifications,
					unit: goods.unit,
					qty: 1,
					price: goods.price,
					amount: goods.price,
					discount: 100,
					cess: 0,
					actualPrice: goods.price,
					actualAmount: goods.price,
					description: "",
				};
				this.$forceUpdate();
			},
			onCustomerChange(e) {
				const customer = e[0];
				this.dataForm.customerName = customer.text;
			},
			count(row) {
				//金额 = 数量*单价
				row.amount = this.jnpf.toDecimal(
					parseFloat(row.price) * parseFloat(row.qty)
				);
				//折扣价 = (单价*折扣)
				var discountPrice = row.price * (row.discount / 100);
				//实际单价 = 折扣价 * (1 + (税率 / 100))
				row.actualPrice = this.jnpf.toDecimal(
					discountPrice * (1 + row.cess / 100)
				);
				//实际金额
				row.actualAmount = this.jnpf.toDecimal(
					parseFloat(row.actualPrice) * parseFloat(row.qty)
				);
				this.$forceUpdate();
			},
		},
	};
</script>